package com.example.gulimallproduct.service.impl;

import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.utils.R;
import com.example.gulimallproduct.entity.CategoryBrandRelationEntity;
import com.example.gulimallproduct.feigen.ThirdPartyFeignClient;
import com.example.gulimallproduct.request.PageBrandRequest;
import com.example.gulimallproduct.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.gulimallproduct.dao.BrandDao;
import com.example.gulimallproduct.entity.BrandEntity;
import com.example.gulimallproduct.service.BrandService;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;

@Service("brandService")
@Slf4j
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Autowired
    private ThirdPartyFeignClient thirdPartyFeignClient;


    @Override
    @Transactional
    public R del(Long[] brandIds) {
        for (Long brandId : brandIds) {
            long count = categoryBrandRelationService.lambdaQuery().eq(CategoryBrandRelationEntity::getBrandId, brandId).count();
            if (count > 0) {
                return R.error("品牌下有关联分类，不能删除");
            }
        }
        //获取OSS文件URL
        List<String> logoUrlList = Lists.newArrayList(brandIds).stream().map(brandId -> {
            BrandEntity brandEntity = lambdaQuery().select(BrandEntity::getLogo).eq(BrandEntity::getBrandId, brandId).one();
            if (Objects.nonNull(brandEntity)) {
                return brandEntity.getLogo();
            }
            return null;
        }).filter(Objects::nonNull).toList();

        this.removeByIds(Lists.newArrayList(brandIds));
        //删除OSS文件
        logoUrlList.forEach(logo -> thirdPartyFeignClient.del(logo));
        return R.ok();
    }

    @Override
    public R selectPage(PageBrandRequest params) {
        Page<BrandEntity> page = page(new Page<>(params.getPageNum(), params.getPageSize()),
                new LambdaQueryWrapper<BrandEntity>().likeRight(BrandEntity::getName, params.getBrandName()));
        return R.ok().put("data", page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateBrand(BrandEntity brandEntity) {
        updateById(brandEntity);
        categoryBrandRelationService.lambdaUpdate()
                .set(CategoryBrandRelationEntity::getBrandName, brandEntity.getName())
                .eq(CategoryBrandRelationEntity::getBrandId, brandEntity.getBrandId())
                .update();
        return R.ok();
    }

}